﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Entity;
using School.Entities;
using School.Common;
using School.Accounting;

namespace School.Pages.Accounting
{
    public partial class PayIncompleteVoucher : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            long number;

            if(!long.TryParse(Request.QueryString["paymentOrder"],out number))
                Response.Redirect("../Organization/no_privelege.aspx");

            var paymentOrder = Voucher.GetPaymentOrdersByNumber(number).First();//PaymentOrder.GetByNumber(number).First();

            var paid = PaymentOrder.GetPaidValue(number);
            IEnumerable<DiscountObject> discounts = PaymentOrder.GetUnappliedDiscounts(paymentOrder);            

            LblPaymentOrder.Text = paymentOrder.Value.ToString();
            LblModefiedPaymentOrder.Text = paymentOrder.Value.ToString();
            LblRemaining.Text = (paymentOrder.Value - paid).ToString();
            lblPaid.Text = paid.ToString();

            if (!IsPostBack)
            {
                GridView1.DataSource = discounts.Where(x => x.Value <= paymentOrder.Value);
                GridView1.DataBind();
            }
        }

        protected string GetType(object type)
        {
            return type.ToString() == "1" ? "مبلغ" : "نسبة";
        }

        protected string GetValue(object type, object value)
        {
            if (type.ToString() == "1")
                return value.ToString() + " جنيه";
            else
            {
                var x = String.Format("{0:0.00}", value);
                return x + " جنيه";
            }
        }

        protected List<ACC_Payment_Order_Discounts> CalculateValue()
        {
            decimal fv = decimal.Parse(LblPaymentOrder.Text);
            //decimal ov = decimal.Parse(LblPaymentOrder.Text);

            List<ACC_Payment_Order_Discounts> discounts = new List<ACC_Payment_Order_Discounts>();

            foreach (GridViewRow x in GridView1.Rows)
            {
                CheckBox ch = (CheckBox)x.FindControl("ChkDiscount");
                Label type = (Label)x.FindControl("LblType");
                Label value = (Label)x.FindControl("LblValue");
                Label DId = (Label)x.FindControl("LblDiscountId");

                if (ch.Checked)
                {
                    ACC_Payment_Order_Discounts discount = new ACC_Payment_Order_Discounts();

                    discount.Type = byte.Parse(type.Text);
                    discount.Discount_Id = long.Parse(DId.Text);
                    discount.Value = decimal.Parse(value.Text);

                    discounts.Add(discount);

                    fv = fv - decimal.Parse(value.Text);

                }
            }

            LblModefiedPaymentOrder.Text = fv > 0 ? fv.ToString() : "0.00";
            LblRemaining.Text = fv > 0 ? (fv - decimal.Parse(lblPaid.Text)).ToString() : "0.00";

            return discounts;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            CalculateValue();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            var discounts = CalculateValue();

            long number;

            if (!long.TryParse(Request.QueryString["paymentOrder"], out number))
                return;            

            var value = decimal.Parse(LblModefiedPaymentOrder.Text);
            
            var paymentOrder = Voucher.GetPaymentOrdersByNumber(PaymentOrder.UpdatePaymentOrderValue(number, value).Number);            

            PaymentOrder.ApplyDiscounts(number, discounts);
            
            var pos = LatePayment.GetVouchers(Student: paymentOrder.First().Student_Id);

            if (pos.Count() > 0)
                pos = pos.Where(x => x.PaymentOrderNumber == number).ToList();

            List<IncompleteVoucherObject> po = new List<IncompleteVoucherObject>();

            foreach (var p in pos)
            {
                p.OriginalValue = decimal.Parse(LblModefiedPaymentOrder.Text);
                p.RemainingValue = decimal.Parse(LblRemaining.Text);

                po.Add(p);
            }

            IncompleteVouchersGrid.DataSource = po;
            IncompleteVouchersGrid.DataBind();
        }
    }
}